<?php

$api_service_path = __DIR__ . "/../../../../api/";
ob_start();
require_once($api_service_path . "public/index.php");
ob_end_clean();

DB::connection()->disableQueryLog();

//修改 id为1 的会员经验要求为0
MemberOrdinary::where("id",Config::get('user.def_member'))->update(array("experience"=>0));
echo '会员经验修改';

/**
 * step 1
 * 修改所有的个人用户 经验值归零 会员为1
 */
DB::table('user_member')->truncate();
DB::table('user_experience_flow')->truncate();
Users::where("type",1)->update(array("experience"=>0,"member"=>Config::get('user.def_member')));
echo '经验值归零';

/**
 * step 2
 * 循环用户发放会员
 */
$not_id=array(1000005413,1000005451,1000013311,1000001732,1000000151,2467,1000001947,1000000240,2554,10186,1406,11592,1000002310,1000004198,1000005458,1000000906,1000002347,1000007063,1000004866,1000001629,1000005568,2612,1000006825,3028,1000001930,1000004906,1311,2763,2831,1000001709,1000000562,2971,1000005759,1293,2889,7955,1000002802,1000000248,1000000191,1000000585,2802,1000004452,1000010555,1000007270,1000008373,8622,1000003440,9026,697,2786,1000002675,1920,1000003248,867,1000009881,1000014484,1000003202,1642,1000008759,1930,1648,2999,1908,2132,1360,26401,11532,1000003577,1000008940,14813,11785,1000000882,1000002172,1000000171,1000002024,1611,1000008636,11252,1000000161,1000000816,1528,17648,1435,1000001839,1000007764,11718,11573,10942,1000001661,1000004424,19847,1000009158,2227,9684,1700,1000004620,1000000194,10154,26370,26403,1000021152,1000008245,1000012980,1000009212,1000006284,6298,1000013454,1372,14825,9330,1195,15017,11696,1000000229,11365,1000006617,1000011978,1000000868,1000002924,1000000863,1000000343,2767,1000002298,1000002338,5424,27969,12786,5422,11674,1000009184,2758,1643,2067,7987,1000005777,1000001328,12223,1000002644,1000011207,1000000296,2598,683,11424,2807,1000007551,11954,2557,1000000784,1000001008,1000005470,10840,1000002652,11701,5733,1000014698,7087,31187,1000006105,3025,1000000188,1000005974,1926,1000003474,1000001730,1000001981,1000006736,11759,1000006586,1000006407,704,1000007934,5754,11622,2823,1000000705,1000000104,2688,710,1000013380,5991,24666,2855,11215,1000000037,11800,1000014486,31215,1000003292,1000005710,26371,11621,30943,1000010622,2788,1000013794,1000003199,11726,1000005464,27860,11287,11613,1000000804,1000000895,24711,1000000969,26326,11464,27152,1000013529,1000010453,31411,1000004103,25972,11329,1000008076,1000009652,9893,1000007473,11760,1000000959,1000002157,1000011298,1000007887,1000001110,11210,1000004131,1174,1404,737,1000004148,1151,1158,1000013206,1000012143,1898,1000011266,1000004016,10094,1000033240,5126,1000003394,1000007087,11511,1000000953,27592,1000006201,1000002268,1000006029,1000002251,1214,1000007376,1000005628,1431,1000006198,1000014547,1317,1000021980,1000008710,1921,1000000192,1000013117,8788,1000007777,1000004935,1000012907,2968,1160,1000002325,1000011419,16129,1427,1000006619,2967,1000005718,1000012252,1000012500,1000003486,1000002558,1000000478,730,1000004502,1000001171,1000010131,7586,7369,1000001630,1368,13040,1000007917,1000000529,1000008376,1000010450,17341,9112,1000000486,20095,1000002085,2229,1000003967,1000000020,1000008593,1000000000,1879,11336,6005,1000007887,1000000228,11205,11210,6084,24867,6397,26252,24866,24737,11228,11301,29742,11284,11212,26233,6432,6830,6394,24869,11249);//不计经验的用户

$user=Users::where("type",1)->select("id");
$user->chunk(1000, function($user) use ($not_id) { 
	$id=0;   	
	foreach ($user as $_u) {
	    if(in_array($_u->id, $not_id)){//如果在系统中
	    	continue;
		}
		$temp_experience=0;
		$order_amount=Orders::leftJoin("order_pay_record","orders.order_id","=","order_pay_record.order_id")->whereIn('orders.status',array(1018,1017,1217,1317,1417))->where('orders.pay_time',">","2016-06-01 00:00:00")->where("orders.pay_user_id",$_u->id)->where("order_pay_record.status",1)->whereIn("order_pay_record.pay_type",OrderPay::$ei_pay_type)->sum("order_pay_record.amount");
		$service_amount=ServiceCardOrder::leftJoin("service_card_order_pay_record","service_card_order.order_id","=","service_card_order_pay_record.order_id")->whereIn('service_card_order.status',array(1018,1017,1217,1317,1417))->where('service_card_order.pay_time',">","2016-06-01 00:00:00")->where("service_card_order.user_id",$_u->id)->where("service_card_order_pay_record.status",1)->whereIn("service_card_order_pay_record.pay_type",ServiceCardOrderPayRecord::$ei_pay_type)->sum("service_card_order_pay_record.amount");
		$car_wash_amount=CarWashOrder::leftJoin("car_wash_order_pay_record","car_wash_order.order_id","=","car_wash_order_pay_record.order_id")->whereIn('car_wash_order.status',array(1018,1017,1217,1317,1417))->where('car_wash_order.pay_time',">","2016-06-01 00:00:00")->where("car_wash_order.user_id",$_u->id)->where("car_wash_order_pay_record.status",1)->whereIn("car_wash_order_pay_record.pay_type",CarWashOrderPayRecord::$ei_pay_type)->sum("car_wash_order_pay_record.amount");
		$return_order_amont=ReturnOrder::leftJoin("orders","orders.order_id","=","return_order.order_id")
			->leftJoin("return_order_record","return_order.id","=","return_order_record.return_order_id")
            ->where("return_order.status",1)->where("orders.pay_user_id",$_u->id)->whereIn("return_order_record.pay_type",OrderPay::$ei_pay_type)->sum("return_order_record.amount");
        $return_wash_amount=ReturnCarwashOrder::leftJoin("car_wash_order","car_wash_order.order_id","=","return_carwash_order.order_id")
        	->leftJoin("return_washorder_record","return_carwash_order.id","=","return_washorder_record.return_washorder_id")
            ->where('return_carwash_order.status',1)->where("car_wash_order.user_id",$_u->id)->whereIn("return_washorder_record.pay_type",CarWashOrderPayRecord::$ei_pay_type)->sum("return_washorder_record.amount");
        $return_service_amount=ReturnServiceCarOrder::leftJoin("service_card_order","service_card_order.order_id","=","return_service_card_order.order_id")->leftJoin("return_service_card_record","return_service_card_order.id","=","return_service_card_record.return_order_id")->where('return_service_card_order.status',1)->where("service_card_order.user_id",$_u->id)->whereIn("return_service_card_record.pay_type",ServiceCardOrderPayRecord::$ei_pay_type)->sum("return_service_card_record.amount");
        
        $temp_experience=floor(($order_amount+$service_amount+$car_wash_amount-$return_order_amont-$return_wash_amount-$return_service_amount)/100);
        if($temp_experience>0){
        	UserExperienceFlow::record($_u->id,0,"",1,1,$temp_experience,'期初预制经验');
            Users::checkMember($_u->id);
        }
        $id=$_u->id;
	}           
	echo '1000条完毕';   
	Log::info('1000条完毕.ID:'.$id);  	
}); 

// select * from coupons where coupon_base_info_id in(162,163,164,165,166,167,168,169,170,171,172,173,174,175,176);
// update coupons set `status`=1 , received_user_id=0 , get_time='0000-00-00 00:00:00',end_date=null where coupon_base_info_id in(141,140,143);



